home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Freeware / Griffith 0.9.8 / griffith-0.9.8-win32.exe / {app} / lib / gdebug.py < prev    next >
Text File  |  2008-11-17  |  4KB  |  131 lines

  1. # -*- coding: UTF-8 -*-
  2.  
  3. __revision__ = '$Id: gdebug.py 1029 2008-11-10 16:58:54Z mikej06 $'
  4.  
  5. # Copyright (c) 2005-2008 Vasco Nunes, Piotr O┼╝arowski
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU Library General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  20.  
  21. # You may use and distribute this software under the terms of the
  22. # GNU General Public License, version 2 or later
  23.  
  24. import gtk
  25. import pygtk
  26.  
  27. class GriffithDebug:
  28.     debug_mode = None
  29.     
  30.     def __init__(self, mode=False):
  31.         self.debugWindow = None
  32.         self.set_debug(mode)
  33.  
  34.     def set_debug(self, mode=True):
  35.         self.debug_mode = mode
  36.         self.check_for_window()
  37.         if not (self.debugWindow is None or self.debugWindow == False):
  38.             if mode:
  39.                 self.debugWindow.show()
  40.             else:
  41.                 self.debugWindow.hide()
  42.  
  43.     def check_for_window(self):
  44.         # on windows systems all output to stdout goes to a black hole
  45.         # if py2exe is used.
  46.         # so we use a normal window and redirect output from sys.stderr
  47.         # and sys.stdout. it is easier to use for windows users.
  48.         try:
  49.             if self.debug_mode and self.debugWindow is None:
  50.                 import gutils, sys
  51.                 if gutils.is_windows_system():
  52.                     self.debugWindow = DebugWindow(None)
  53.                     sys.stderr = sys.stdout = DebugWindowRedirector(self.debugWindow)
  54.                 else:
  55.                     self.debugWindow = False
  56.         except:
  57.             pass
  58.  
  59.     def show(self, txt):
  60.         if self.debug_mode:
  61.             print txt.encode('utf8')
  62.  
  63. #
  64. # used on windows systems
  65. # shows a windows with a text view which displays the
  66. # debug output provided by DebugWindowRedirector
  67. #
  68. class DebugWindow:
  69.     def __init__(self, window):
  70.         self.dialog = gtk.Dialog('Debug Window', window, gtk.DIALOG_MODAL, ())
  71.         self.dialog.set_destroy_with_parent(True)
  72.         self.dialog.set_transient_for(window)
  73.         self.dialog.set_modal(False)
  74.         self.textview = gtk.TextView()
  75.         self.textview.set_editable(False)
  76.         self.textview.set_wrap_mode(gtk.WRAP_WORD_CHAR)
  77.         self.textview.set_scroll_adjustments(gtk.Adjustment(1.0, 1.0, 100.0, 1.0, 10.0, 10.0), gtk.Adjustment(1.0, 1.0, 100.0, 1.0, 10.0, 10.0))
  78.         self.scrolledwindow = gtk.ScrolledWindow(None, None)
  79.         self.scrolledwindow.add(self.textview)
  80.         self.dialog.vbox.pack_start(self.scrolledwindow)
  81.         self.dialog.set_default_size(640, 480)
  82.     def show(self):
  83.         self.dialog.show_all()
  84.     def hide(self):
  85.         self.dialog.hide_all()
  86.     def close(self):
  87.         self.dialog.destroy()
  88.     def add(self, txt):
  89.         if txt is not None:
  90.             buffer = self.textview.get_buffer()
  91.             buffer.insert(buffer.get_end_iter(), txt, -1)
  92.  
  93. #
  94. # used on windows system
  95. # redirects sys.stderr and sys.stdout to a debug window and a file
  96. # 'griffith_debug.txt' in the home directory (My documents)
  97. #
  98. class DebugWindowRedirector(object):
  99.     softspace = 0
  100.     def __init__(self, debugWindow):
  101.         self.debugWindow = debugWindow
  102.         #
  103.         # redirected output to a debug file in the home directory because
  104.         # users with restricted system rights can't write to the installation directory
  105.         # which is the default if py2exe is used
  106.         #
  107.         import winshell, os
  108.         from win32com.shell import shellcon, shell
  109.         from locale import getdefaultlocale
  110.         defaultLang, defaultEnc = getdefaultlocale()
  111.         if defaultEnc is None:
  112.             defaultEnc = 'UTF-8'
  113.         self.debugFileName = os.path.join(shell.SHGetFolderPath(0, shellcon.CSIDL_PERSONAL, 0, 0), 'griffith_debug.txt').decode(defaultEnc)
  114.         try:
  115.             f = open(self.debugFileName, 'w')
  116.             f.close()
  117.         except:
  118.             pass
  119.     def write(self, text):
  120.         self.debugWindow.add(text)
  121.         try:
  122.             f = open(self.debugFileName, 'a')
  123.             try:
  124.                 f.write(text)
  125.             finally:
  126.                 f.close()
  127.         except:
  128.             pass
  129.     def flush(self):
  130.         pass
  131.